import 'dart:async';

import 'package:inmotion_pro/service/global_service.dart';
import 'package:get/get.dart';

import 'state.dart';

class VehicleErrorController extends GetxController {
  // 状态
  final VehicleErrorState state = VehicleErrorState();

  // 车辆实时错误订阅
  StreamSubscription? _realTimeErrorSubscription;

  @override
  void onInit() {
    super.onInit();

    // 订阅车辆实时错误
    _realTimeErrorSubscription =
        GlobalService.to.realtimeError.listen((realtimeError) {
      // 处理实时错误
      state.errorList.value = realtimeError.vehicleErrors;
    });
  }

  @override
  void onClose() {
    // 取消订阅
    _realTimeErrorSubscription?.cancel();
    _realTimeErrorSubscription = null;
    super.onClose();
  }
}
